package com.newcoin.xgb.controller;

import com.newcoin.xgb.dto.QueryParameterDto;
import com.newcoin.xgb.dto.SendSmsCodeDto;
import com.newcoin.xgb.dto.UpdateAppDto;
import com.newcoin.xgb.entity.*;
import com.newcoin.xgb.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/xgb")
@Slf4j
@Api(description = "乐刷公共接口")
public class CommonController {
    @Autowired
    private CommonService service;


    @ApiOperation(value = "发送验证码")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "key", value = "签名", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/sendSmsCode")
    public Result sendSmsCode(SendSmsCodeDto sendSmsCodeDto) {
        try {
            return service.sendSmsCode(sendSmsCodeDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setMessage("发送短信失败");
            result.setCode(500);
            return result;
        }
    }

    @ApiOperation(value = "上传文件")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "__file"),
                    @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/uploadFile")
    public Result uploadFile(MultipartFile file, String type) {
        try {
            return service.uploadFile(file, type);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setMessage("上传失败");
            result.setCode(500);
            return result;
        }
    }

    /**
     * 检查更新
     *
     * @param version
     * @return
     */
    @ApiOperation(value = "检查更新")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "version", value = "版本", required = true)
            }
    )
    @PostMapping(value = "/isUpdate")
    public Map<String, Object> isUpdate(String version) {
        try {
            return service.isUpdate(version);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("status", 500);
            result.put("msg", "系统错误");
            return result;
        }
    }

    @ApiOperation(value = "首页轮播图")
    @GetMapping(value = "/getCarousel")
    public Result<List<XgbIndexCarousel>> getCarousel(){
        try {
            return service.getCarousel();
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<List<XgbIndexCarousel>> result = new Result();
            result.setMessage("获取轮播图失败");
            result.setCode(500);
            return result;
        }
    }

    @ApiOperation(value = "首页抢购区")
    @GetMapping(value = "/getArea")
    public Result<List<XgbArea>> getArea(){
        try {
            return service.getArea();
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<List<XgbArea>> result = new Result();
            result.setMessage("获取抢购区失败");
            result.setCode(500);
            return result;
        }
    }

    @ApiOperation(value = "首页公告")
    @GetMapping(value = "/getAnnouncement")
    public Result<List<XgbIndexAnn>> getAnnouncement(){
        try {
            return service.getAnnouncement();
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<List<XgbIndexAnn>> result = new Result();
            result.setMessage("获取公告失败");
            result.setCode(500);
            return result;
        }
    }

    @ApiOperation(value = "获取公告详情")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "公告id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getAnnouncementBuyId")
    public Result<XgbIndexAnn> getAnnouncementBuyId(QueryParameterDto queryParameterDto){
        try {
            return service.getAnnouncementBuyId(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<XgbIndexAnn> result = new Result();
            result.setMessage("获取公告详情失败");
            result.setCode(500);
            return result;
        }
    }

    @ApiOperation(value = "获取客服链接")
    @GetMapping(value = "/getCustomerService")
    public Result<XgbCustomerService> getCustomerService(){
        try {
            return service.getCustomerService();
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<XgbCustomerService> result = new Result();
            result.setMessage("获取客服链接失败");
            result.setCode(500);
            return result;
        }
    }

}
